import {
	defineStore
} from 'pinia';
import api from '@/api/login.js';

export const useAuthStore = defineStore('auth', {
	state: () => ({
		inputColorConfigToUserName: {
			color: '#000000',
			disableColor: '#eee',
			borderColor: '#b4b4b4',
		},
		inputColorConfigToPassWord: {
			color: '#000000',
			disableColor: '#eee',
			borderColor: '#b4b4b4',
		},
		user: {
			username: '',
			password: '',
		},
	}),
	actions: {
		UserNamePassWrodFocusBorderStyle() {
			this.inputColorConfigToUserName.borderColor = '#3cb79a';
		},
		UserNamePassWrodBlurBorderStyle() {
			this.inputColorConfigToUserName.borderColor = '#b4b4b4';
		},
		PassWordBlurBorderStyle() {
			this.inputColorConfigToPassWord.borderColor = '#b4b4b4';
		},
		async login() {
			const res = await api.pwdLogin({
				name: this.user.username,
				password: this.user.password,
			});
			uni.setStorage({
				key: 'Authorization',
				data: 'Bearer ' + res.data.token,
			});
			uni.setStorage({
				key: 'realName',
				data: res.data.realName,
			});
			uni.setStorage({
				key: 'userName',
				data: res.data.userName,
			});
			uni.showToast({
				title: '登录成功',
				icon: 'success',
				position: 500,
			});
			uni.switchTab({
				url: '/pages/ListContainers/ListContainers',
			});
		},
	},
});